﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Catering.View_Operaction;
using System.Text.RegularExpressions;

namespace Catering.Catering_Page.system
{
    public partial class NoticeInfo : System.Web.UI.Page
    {
        String FileNameList = string.Empty;
        // 属性注入
        Catering_Rules.Announcement Ann = new Catering_Rules.Announcement();
        IList<String> fileUploadList = new List<String>();

        // 页面 Load 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (!string.IsNullOrEmpty(Request.QueryString["status"]) && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    string status = Request.QueryString["status"];
                    string strid = Request.QueryString["id"];
                    if (status.Equals("del"))
                    {
                        int id = 0;
                        Int32.TryParse(strid, out id);
                        if (id > 0)
                        {
                            #region 删除相关内容（图片和附件）

                            Catering_Model.Announcement model = Ann.GetModel(id);

                            if (model != null && !string.IsNullOrEmpty(model.Conent))
                            {
                                string temcon = model.Conent;
                                string reg = @"<img[^>]*src=(""?(?<url>[^""]*)""|'?(?<url>[^']*)')?[^>]*>";
                                Regex rgex = new Regex(reg, RegexOptions.IgnoreCase);
                                MatchCollection mc = rgex.Matches(temcon);
                                for (int i = 0; i < mc.Count; i++)
                                {
                                    string href = mc[i].Groups["url"].Value.Replace("../..","");
                                    Toolss.DeleteFile(href);
                                }
                            }

                            if (model != null && !string.IsNullOrEmpty(model.temp1))
                            {
                                string fileList = model.temp1;
                                if (!string.IsNullOrEmpty(fileList)) fileUploadList = fileList.Split('|');
                                foreach (var item in fileUploadList)
                                {

                                    if (!string.IsNullOrEmpty(item))
                                    {
                                        string href = "/UpFile/" + item;
                                        Toolss.DeleteFile(href);
                                    }
                                }
                            }

                            #endregion

                            Ann.Delete(strid);
                        }

                    }
                }
                DataTable dt = Ann.GetList().Tables[0];
                dt.Columns.Add(new DataColumn("tempid"));
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["tempid"] = i + 1;
                }
                RepeaterDataBind(dt, NoticeRept);
            }
        }

        /// <summary>
        /// 为隐藏层内控件赋值
        /// </summary>
        /// <param name="model"></param>
        //private void SetDateToControls(Catering_Model.Announcement model)
        //{
        //    txt_Title.Text = model.Title;
        //    ftxt_Conent.Text = model.Conent;
        //    div_hi_time.Value = model.Release.ToString("yyyy-MM-dd");
        //    div_hi_userid.Value = model.UserId.ToString();
        //    div_hi_id.Value = model.id.ToString();
        //    string fileList = model.temp1;
        //    string nameList = string.Empty;
        //    if (!string.IsNullOrEmpty(fileList))
        //    {
        //        fileUploadList = fileList.Split('|');
        //    }
        //    foreach (var item in fileUploadList)
        //    {

        //        if (!string.IsNullOrEmpty(item))
        //        {
        //            IList<String> list = new List<String>();
        //            list = item.Split(new string[1] { "@@" }, StringSplitOptions.None);
        //            if (list.Count > 1)
        //            {
        //                nameList = nameList + "  " + list[1];
        //            }
        //        }
        //        allFileName.InnerText = nameList;
        //    }
        //}

        /// <summary>
        /// 清空隐藏层内数据
        /// </summary>
        //private void ClearDataInControls()
        //{
        //    txt_Title.Text = "";
        //    ftxt_Conent.Text = "";
        //    div_hi_time.Value = "";
        //    div_hi_userid.Value = "";
        //}

        private void RepeaterDataBind(DataTable dt, Repeater rpt)
        {
            PagedDataSource objPage = new PagedDataSource();
            //设置数据源 DefaultView：获取可能包含筛选视图或游标位置表的自定义视图
            objPage.DataSource = dt.DefaultView;
            //允许分页
            objPage.AllowPaging = true;
            //设置每页个数
            objPage.PageSize = 8;
            //定义变量保存当前页的索引值

            int CurPage;
            //判断是否具有页面跳转的请求
            if (Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            //设置当前页的索引
            objPage.CurrentPageIndex = CurPage - 1;

            //显示状态信息
            lblCurPage.Text = "第" + CurPage.ToString() + "/" + objPage.PageCount + "页";
            //如果当前页面不是首页
            if (!objPage.IsFirstPage)
            {
                //定义“上一页”超级链接的URL为：当前执行页面的虚拟路径，并传递下一页面的索引值 
                InkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

            }
            HF_CurPageURL.Value = HttpUtility.UrlEncode(Request.CurrentExecutionFilePath + "?Page=" + CurPage);
            InkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (objPage.PageCount);
            InkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
            //如果当前页面不是最后一页
            if (!objPage.IsLastPage)
            {
                //定义“下一页”超级链接的URL为：当前执行页面的虚拟路径，并传递下一页面的索引值 
                InkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
            }
            //将PagedDataSource作为数据源
            rpt.DataSource = objPage;

            //进行数据绑定
            rpt.DataBind();
        }

        //protected void btn_Add_Click(object sender, EventArgs e)
        //{
        //    ClearDataInControls();
        //    bg.Style[HtmlTextWriterStyle.Display] = "block";
        //    main.Style[HtmlTextWriterStyle.Display] = "block";
        //    this.btnEdit.Visible = false;
        //    this.btnAdd.Visible = true;
        //}

        //protected void LinkButton1_Click(object sender, EventArgs e)
        //{
        //    bg.Style[HtmlTextWriterStyle.Display] = "none";
        //    main.Style[HtmlTextWriterStyle.Display] = "none";
        //    ClearDataInControls();
        //}

        //protected void btnInfoAdd_Click(object sender, EventArgs e)
        //{
        //    Catering_Model.Announcement model = new Catering_Model.Announcement();
        //    model.Title = txt_Title.Text;
        //    model.Conent = ftxt_Conent.Text;
        //    model.Release = DateTime.Now;
        //    string fileList = Toolss.GetCookie("FILENAMELIST");
        //    if (!string.IsNullOrEmpty(fileList))
        //    {
        //        fileList = Bind_Data_to_Controls.Decode(fileList);
        //        model.temp1 = fileList;
        //    }
        //    else
        //    {
        //        model.temp1 = "";
        //    }
        //    model.temp2 = "";
        //    model.UserId = Convert.ToInt32(Session["UserID"]);
        //    Ann.Add(model);
        //    bg.Style[HtmlTextWriterStyle.Display] = "none";
        //    main.Style[HtmlTextWriterStyle.Display] = "none";
        //    ClearDataInControls();
        //    Response.AddHeader("Refresh", "0");


        //}

        //protected void btnEdit_Click(object sender, EventArgs e)
        //{
        //    Catering_Model.Announcement model = new Catering_Model.Announcement();
        //    model.Title = txt_Title.Text;
        //    model.Conent = ftxt_Conent.Text;
        //    model.Release = DateTime.Parse(div_hi_time.Value.Length < 6 ? DateTime.Now.ToShortDateString() : div_hi_time.Value);
        //    model.UserId = Convert.ToInt32(Session["UserID"].ToString());
        //    model.id = Convert.ToInt32(div_hi_id.Value);
        //    string fileList = Toolss.GetCookie("FILENAMELIST");
        //    if (!string.IsNullOrEmpty(fileList))
        //    {
        //        fileList = Bind_Data_to_Controls.Decode(fileList);
        //        model.temp1 = fileList;
        //    }
        //    if (Ann.Update(model))
        //    {
        //        bg.Style[HtmlTextWriterStyle.Display] = "none";
        //        main.Style[HtmlTextWriterStyle.Display] = "none";
        //        Toolss.WriteCookie("FILENAMELIST", "");
        //        ClearDataInControls();
        //    }
        //}

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            string type = DropDownList1.SelectedItem.Value;
            DataTable dt = Ann.GetListForSearch(type, txt_search.Text);
            dt.Columns.Add(new DataColumn("tempid"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["tempid"] = i + 1;
            }
            RepeaterDataBind(dt, NoticeRept);
        }
    }
}